package de.iceboy.android.goldrush;

import de.iceboy.android.goldrush.data.Hit;
import de.iceboy.android.goldrush.data.HitSet;
import de.iceboy.android.goldrush.view.TargetView;
import android.app.Activity;
import android.content.pm.FeatureInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.Toast;
import android.widget.ZoomControls;

public class StartScreen extends Activity {
	private final class MovementListener implements View.OnTouchListener {
		
		final GestureDetector gd = new GestureDetector(new GestureDetector.SimpleOnGestureListener(){

			@Override
			public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
				// TODO Auto-generated method stub
				Log.d("Fling", velocityX + ", " + velocityY);
				return super.onFling(e1, e2, velocityX, velocityY);
			}

			@Override
			public void onLongPress(MotionEvent e) {
				// TODO Auto-generated method stub
				super.onLongPress(e);
			}

			@Override
			public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
				//return super.onScroll(e1, e2, distanceX, distanceY);
				target.move(distanceX, distanceY);
				target.invalidate();
				Log.d("Goldrush", "scroll");
				return true;
			}

			@Override
			public boolean onDoubleTapEvent(MotionEvent e) {
				if(e.getAction() == 0)
					return true;
				Log.d("Goldrush", "doubletap " + e.getAction()+" " + e.getEventTime());
				target.zoomIn(e.getX(), e.getY());
				return true;
			}

			@Override
			public boolean onSingleTapConfirmed(MotionEvent e) {
				Log.d("Goldrush", "tap");
				Hit h = target.getHit(e.getX(),	e.getY());
				hitSet.AddHit(h);
				Log.d("HIT", "Points: " + h.getPoints());
				target.invalidate();
				return true;
			}
			
		});
		
		@Override
		public boolean onTouch(View v, MotionEvent e) {
			gd.onTouchEvent(e);
			return true;
		}

		
	}

	/** Called when the activity is first created. */

	private TargetView target;
	private HitSet hitSet;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		hitSet = new HitSet();
		setContentView(R.layout.main);
		FrameLayout main = (FrameLayout) findViewById(R.id.main_view);
		target = new TargetView(this);
		target.setHitSet(hitSet);
		main.addView(target);
		Button showCompleteTarget = (Button) findViewById(R.id.btnShow);
		showCompleteTarget.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				target.zoomCompleteOut();
			}
		});
		((Button) findViewById(R.id.btnReset)).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				hitSet.clear();
			}
		});
		main.setOnTouchListener(new MovementListener());
	}
}